package com.atguigu.gulimall.search.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.*;
import java.util.concurrent.FutureTask;

public class ThreadTest {
    public static ExecutorService service = Executors.newFixedThreadPool(10);

    //CompletableFuture 的使用
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main............start..............");

        //无返回值
//        CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 - 2;
//            System.out.println("运行结果：" + i);
//        }, service);

        //有返回值
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 - 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, service);
//        System.out.println("返回值：" + future.get());


        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 - 2;
            System.out.println("运行结果：" + i);
            return i;
        }, service).whenComplete((res,excetion)->{
            System.out.println("异步任务完成了。。。。结果是：" + res + ",异常是：" + excetion);
        });

        System.out.println("main............end..............");

    }

    public static void thread(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main............start..............");
        /**
         * 1.继承Thread
         *         Thread01 thread = new Thread01();
         *         thread.start();//启动线程
         * 2.实现Runnable接口
         *         Runable01 runable = new Runable01();
         *         new Thread(runable).start();
         * 3.实现Callable接口+FutureTask（可以拿到返回结果，可以处理异常）
         *         FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
         *         new Thread(futureTask).start();
         *         //等待整个线程执行完成，获取返回结果
         *         Integer integer = futureTask.get();
         *         System.out.println("返回结果："+integer);
         * 4.线程池
         *      给线程池直接提交任务。
         *       service.execute(new Thread01());
         *      1、创建[ExecutorService]
         *          1）、Executors
         *
         * 区别：
         *      1、2不能得到返回值
         *      1、2、3都不能控制资源
         *      4 可以控制资源，性能稳定
         *
         */


        //当前系统中线程池有1-2个（有些业务线程和核心线程可以分开），每个异步任务，提交给线程池让他自己去执行



        System.out.println("main............end..............");

    }

    public static class Thread01 extends Thread{
        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 - 2;
            System.out.println("运行结果：" + i);
        }
    }

    public static class Runable01 implements Runnable {
        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 - 2;
            System.out.println("运行结果：" + i);
        }
    }

    public static class Callable01 implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 - 2;
            System.out.println("运行结果：" + i);
            return i;
        }
    }
}
